package com.bbx.iotdb.iotdbdemo.jdbc;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;

@Slf4j
@RestController
@RequestMapping("/jdbc")
public class JDBCTest {

    /**
     * 创建连接对象
     *
     * @return
     */
    public Connection getConnection() {
        String driver = "org.apache.iotdb.jdbc.IoTDBDriver";
        String url = "jdbc:iotdb://192.168.10.85:6667/";
        String username = "root";
        String password = "root";
        Connection connection = null;

        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return connection;

    }

    public void outPrintResultSet(ResultSet resultSet) throws SQLException {
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        for (int i = 1; i <= columnCount; i++) {
            String columnName = metaData.getColumnName(i);
            log.info("columnName[{}]", columnName);
        }
        while (resultSet.next()) {
            for(int i =1;i<= columnCount;i++){
                String string = resultSet.getString(i);
                log.info("string[{}]", string);
            }
        }
    }


    /**
     * 创建 storage group
     *
     * @throws SQLException
     */
    @RequestMapping("/setStorageGroup")
    public void setStorageGroup() throws SQLException {
        Connection connection = getConnection();
        Statement statement = connection.createStatement();
        String sql = "set storage group to root.jdbc";
        statement.execute(sql);
        statement.close();
        connection.close();
    }

    /**
     * 创建 timeseries
     *
     * @throws SQLException
     */
    @RequestMapping("/createTimeseries")
    public void createTimeseries() throws SQLException {
        Connection connection = getConnection();
        Statement statement = connection.createStatement();
        String sql = "create timeseries   root.jdbc.wf05.wt05.status with datatype=boolean ,encoding=plain";
        boolean execute = statement.execute(sql);
        statement.close();
        connection.close();
    }


    /**
     * 插入数据
     *
     * @throws SQLException
     */
    @RequestMapping("/createData")
    public void insertData() throws SQLException {
        Connection connection = getConnection();
        Statement statement = connection.createStatement();
        String sql1 = "insert into root.jdbc.wf05.wt05 (timestamp,status) values(105 ,true)";
        String sql2 = "insert into root.jdbc.wf05.wt05 (timestamp,status) values(106 ,true)";
        String sql3 = "insert into root.jdbc.wf05.wt05 (timestamp,status) values(107 ,true)";
        String sql4 = "insert into root.jdbc.wf05.wt05 (timestamp,status) values(108 ,true)";
        String sql5 = "insert into root.jdbc.wf05.wt05 (timestamp,status) values(109 ,true)";
        String sql6 = "insert into root.jdbc.wf05.wt05 (timestamp,status) values(110 ,true)";
        String sql7 = "insert into root.jdbc.wf05.wt05 (timestamp,status) values(111 ,true)";
        String sql8 = "insert into root.jdbc.wf05.wt05 (timestamp,status) values(112 ,false )";
        String sql9 = "insert into root.jdbc.wf05.wt05 (timestamp,status) values(113 ,true)";
        statement.execute(sql1);
        statement.execute(sql2);
        statement.execute(sql3);
        statement.execute(sql4);
        statement.execute(sql5);
        statement.execute(sql6);
        statement.execute(sql7);
        statement.execute(sql8);
        statement.execute(sql9);

        statement.close();
        connection.close();
    }


    /**
     * @throws SQLException
     */
    @RequestMapping("/selectAll")
    public void selectAll() throws SQLException {
        Connection connection = getConnection();
        Statement statement = connection.createStatement();

        //查询 storage group
        String queryStorageGroupSql = "show storage group ";
        statement.execute(queryStorageGroupSql);
        ResultSet resultSet = statement.getResultSet();
        outPrintResultSet(resultSet);

        //查询 timeseries
        String queruTimeseries = "show timeseries";
        statement.execute(queruTimeseries);
        ResultSet resultSet1 = statement.getResultSet();
        outPrintResultSet(resultSet1);
        log.info("[{}]", "查询timeseries end");

        //查询数据
        String queryDataSql = "select * from   root.jdbc.wf05.wt05";
        ResultSet resultSet2 = statement.executeQuery(queryDataSql);
        outPrintResultSet(resultSet2);

        //删除 timeseries
        String deleteTimeriesSql = "delete timeseries root.dev.wf04.wt04.status";
        statement.execute(deleteTimeriesSql);


        statement.close();
        connection.close();
    }




}
